
import {ref} from 'vue'

import ExamInfo from '@/types/examInfo'
import Page from '@/base/page'
import request from '@/base/request'
import { ElMessage } from 'element-plus'
import useDark from './useDark'
export default function(){
    const {dark}=useDark()
    const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
    const showJoinExamDialog=ref(false);
    const disabled=ref<boolean>(false) //禁用滚动加载属性
    const tips=ref<string>("划不下去了？点我加载")
    const examList=ref<Page<ExamInfo>>({
        items:[],
        page:10,
        pageSize:9,
        counts:1,
    })
    const examParm=ref({
        pageNo:1,
        pageSize:9,
        id:"",
        examName:"",
        classBelongId:"",
    })

    const userExamList=ref<Array<ExamInfo>>([
        {
            id:5,
            examName:"text",
            ranking:10,
            peopleNum:100,
            getScore:5,
            score:100
        }
    ])


    const chooseExam=ref<ExamInfo>()

    const now=new Date()

    async function getExamList() {
        disabled.value=true
        tips.value='Loading...'
        const response=(await request.post("/problem/exam/getExamList",examParm.value)).data
        if(response.code===200){
            examParm.value.pageNo++;
            examList.value.page=response.data.page
            examList.value.pageSize=response.data.pageSize
            examList.value.counts=response.data.counts
            
            for(let i=0;i<response.data.items.length;i++){
                examList.value.items.push(response.data.items[i]);
                await delay(200)
            }
            disabled.value=false
            tips.value="划不下去了？点我加载"
        }else{
            ElMessage({
                message:`获取考试列表失败：${response.msg}`,
                grouping:true,
                type:'error'
            })
        }
    }


    async function signUpExam() {
        const response=(await (request.get(`/problem/exam/signUpExam/${chooseExam.value.id}`))).data
        if(response.code===200){
            ElMessage.success("报名成功")
            showJoinExamDialog.value=false;
            let index=-1;
            index=examList.value.items.findIndex(p=>p.id===chooseExam.value.id)
            if(index!==-1){
                examList.value.items[index].status=(examList.value.items[index].status===3?4:2)
                examList.value.items[index].peopleNum++;
            }
        }else{
            ElMessage({
                message:`报名失败：${response.msg}`,
                grouping:true,
                type:'error'
            })
        }
    }

    async function cancelExam() {
        const response=(await (request.get(`/problem/exam/cancelExam/${chooseExam.value.id}`))).data
        if(response.code===200){
            ElMessage.success("取消成功")
            showJoinExamDialog.value=false
            examParm.value.pageNo=1;
            examList.value.items=[];
            load()
        }else{
            ElMessage({
                message:`取消失败：${response.msg}`,
                grouping:true,
                type:'error'
            })
        }
    }

    async function getUserExamList(userId) {
        const response=(await (request.get(`/problem/exam/getUserExamList/${userId}`))).data
        if(response.code===200){
            userExamList.value=response.data
        }else{
            ElMessage.error(`获取用户列表失败,${response.msg}`)
        }
    }



    function load(){
        if(disabled.value===true)return
        if(examParm.value.pageNo>examList.value.page){
            tips.value='已经到底了'
            return
        }
        

        getExamList()
        
    }


    function getBackGround(status:number){
        if(dark.value){
            switch (status) {
                case 1:
                  return { borderColor: "rgb(102.2, 177.4, 255)" }; 
                case 2:
                  return { borderColor: "rgb(133.4, 206.2, 97.4)" }; 
                case 3:
                  return { borderColor: "rgb(235, 180.6, 99)" }; 
                case 4:
                  return { borderColor: "rgb(235, 180.6, 99)" }; 
                default:
                  return { borderColor: "rgb(32.4, 32.7, 33.3)" };
            }
        }else{
            switch (status) {
                case 1:
                  return { backgroundColor: "rgb(159.5, 206.5, 255)" }; 
                case 2:
                  return { backgroundColor: "rgb(148.6, 212.3, 117.1)" }; 
                case 3:
                  return { backgroundColor: "rgb(237.5, 189.9, 118.5)" }; 
                case 4:
                    return { backgroundColor: "rgb(237.5, 189.9, 118.5)" }; 
                default:
                  return { backgroundColor: "rgb(221.7, 222.6, 224.4)" };
            }            
        }

    }

    function getBackGround2(allScore:number,getScore:number){
        if(getScore<allScore*0.6){
            return { color: "rgb(196, 86.4, 86.4)" }; 
        }else if(getScore<allScore*0.8){
            return { color: "rgb(184, 129.6, 48)" }; 
        }else{
            return { color: "rgb(51.2, 126.4, 204)" }; 
        }
    }


    function clickCard(status:number){
        if(status<=4)showJoinExamDialog.value=true;

    }




    function getRemainingTime(index){
        if(examList.value.items[index].status>2)return '';
        let rsecond=examList.value.items[index].rsecond;
        if(rsecond===undefined || rsecond===null){
            let beginTimecp=new Date(examList.value.items[index].beginTime)
            rsecond=Math.floor((beginTimecp.getTime()-now.getTime())/1000)
        }
        rsecond--;
        examList.value.items[index].rsecond=rsecond;
        if(rsecond<0)examList.value.items[index].status=3;
        let day=Math.floor(rsecond/24/60/60);
        let hour=Math.floor(rsecond/60/60%60)
        let minutes=Math.floor(rsecond/60%60);
        let seconds=rsecond%60;
        examList.value.items[index].remaining = (day>0?day+'天':'')+(hour>=0?hour+'时':'')+(minutes>=0?minutes+'分':'')+(seconds>=0?seconds+'秒':'')
    }


    return {dark,examList,examParm,disabled,tips,showJoinExamDialog,chooseExam,userExamList
        ,getUserExamList,cancelExam,signUpExam,getRemainingTime,clickCard,getBackGround,getBackGround2,load,getExamList}
}